﻿using UnityEngine;
using UnityEngine.UI;
public class AdminTools : MonoBehaviour {
	[SerializeField] Button hidenButton;
	[SerializeField] Button bugButton;
	[SerializeField] Button immortalButton;
	[SerializeField] Button unlockAllLevelButton;
	[SerializeField] Button resetDataButton;
	[SerializeField] GameObject adminToolsButton;
	private int tempLevel;

	private int count;

	private void Awake()
	{
		immortalStage = 0;
		unlockLevelStage = 0;
		tempLevel = GameController.gameLevel;
		hidenButton.onClick.AddListener (CallAdminMode);
		bugButton.onClick.AddListener (ShowAdminTool);
		immortalButton.onClick.AddListener (Immortal);
		unlockAllLevelButton.onClick.AddListener (UnlockLevel);
		resetDataButton.onClick.AddListener (ResetData);
	}

	private void Start()
	{
		bugButton.gameObject.SetActive (false);
		adminToolsButton.SetActive (false);
	}

	private void CallAdminMode()
	{
		count++;
		if (count == 10) {
			count = 0;
			hidenButton.gameObject.SetActive (false);
			bugButton.gameObject.SetActive (true);
		}
	}
	private void ShowAdminTool()
	{
		if (!adminToolsButton.activeSelf) {
			adminToolsButton.SetActive (true);
		} else {
			hidenButton.gameObject.SetActive (true);
			bugButton.gameObject.SetActive (false);
			adminToolsButton.SetActive (false);
		}
	}

	public static int immortalStage
	{
		get { return PlayerPrefs.GetInt ("immortal");}
		set { PlayerPrefs.SetInt ("immortal", value);}
	}
	private void Immortal()
	{
		if (immortalStage == 0) {
			immortalStage = 1;
			immortalButton.GetComponent<Image> ().color = Color.red;
		} else {
			immortalStage = 0;
			immortalButton.GetComponent<Image> ().color = Color.white;
		}
	}
	public static int unlockLevelStage
	{
		get { return PlayerPrefs.GetInt ("unlock");}
		set { PlayerPrefs.SetInt ("unlock", value);}
	}
	private void UnlockLevel()
	{
		if (unlockLevelStage == 0) {
			unlockLevelStage = 1;
			unlockAllLevelButton.GetComponent<Image> ().color = Color.red;
			PlayerPrefs.SetInt ("level", 15);
			UIController.SpawnLevel (false);
		} else {
			unlockLevelStage = 0;
			unlockAllLevelButton.GetComponent<Image> ().color = Color.white;
			PlayerPrefs.SetInt ("level", tempLevel);
			UIController.SpawnLevel (false);
		}
	}
	private void ResetData()
	{
		PlayerPrefs.DeleteAll ();
		Application.LoadLevel (Application.loadedLevel);
	}
}
